package com.xbongbong.workflow.service.strategy.data.impl;

import com.xbongbong.paas.domain.entity.PaasFormDataEntity;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.enums.IndexTypeEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.formdata.pojo.SaasNeedRedundantAttrPojo;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.PaasFormSubDataModel;
import com.xbongbong.workflow.domain.entity.WorkflowBackupDataEntity;
import com.xbongbong.workflow.feign.WorkflowWorkOrderV2FeignClient;
import com.xbongbong.workflow.model.WorkflowBackupDataModel;
import com.xbongbong.workflow.pojo.Data;
import com.xbongbong.workflow.pojo.WorkflowTransferPOJO;
import com.xbongbong.workflow.pojo.workorderv2.dto.WorkflowWorkOrderV2ApprovalFailedDTO;
import com.xbongbong.workflow.pojo.workorderv2.dto.WorkflowWorkOrderV2SaveDTO;
import com.xbongbong.workflow.pojo.workorderv2.dto.WorkflowWorkOrderV2UpdateBackDTO;
import com.xbongbong.workflow.pojo.workorderv2.dto.WorkflowWorkOrderV2UpdateDTO;
import com.xbongbong.workflow.pojo.workorderv2.vo.WorkflowWorkOrderV2UpdateBackVO;
import com.xbongbong.workflow.service.strategy.data.AbstractWorkflowDataStrategy;
import com.xbongbong.workflow.service.strategy.data.WorkflowDataProcessStrategy;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
import java.util.Objects;

/**
 * @author 李翔
 * @date 2022/11/17
 * @description
 */
@Service("workflowReceiptOrderStrategy")
public class WorkflowReceiptOrderStrategy extends AbstractWorkflowDataStrategy implements WorkflowDataProcessStrategy {

    private static final Logger LOG = LoggerFactory.getLogger(WorkflowReceiptOrderStrategy.class);

    @Resource
    private WorkflowBackupDataModel workflowBackupDataModel;
    @Resource
    private PaasFormSubDataModel paasFormSubDataModel;
    @Resource
    private WorkflowWorkOrderV2FeignClient workflowWorkOrderV2FeignClient;

    @Override
    public List<Integer> type() {
        return Collections.singletonList(XbbRefTypeEnum.RECEIPT_ORDER.getCode());
    }

    @Override
    public void save(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        WorkflowWorkOrderV2SaveDTO saveDTO = new WorkflowWorkOrderV2SaveDTO();
        saveDTO.setCorpid(workflowTransferPOJO.getCorpid());
        saveDTO.setUser(workflowTransferPOJO.getUser());
        saveDTO.setData(workflowTransferPOJO);
        saveDTO.setBusinessType(XbbRefTypeEnum.RECEIPT_ORDER.getCode());
        workflowWorkOrderV2FeignClient.saveAfterWorkflow(saveDTO, LocaleContextHolder.getLocale().toString());
    }

    @Override
    public void update(WorkflowTransferPOJO workflowTransferPOJO, SaasNeedRedundantAttrPojo saasNeedRedundantAttrPojo) throws XbbException {
        WorkflowWorkOrderV2UpdateDTO saveDTO = new WorkflowWorkOrderV2UpdateDTO();
        saveDTO.setTaskId(workflowTransferPOJO.getTaskId());
        saveDTO.setWorkflowTransferPOJO(workflowTransferPOJO);
        saveDTO.setBusinessType(XbbRefTypeEnum.RECEIPT_ORDER.getCode());
        workflowWorkOrderV2FeignClient.updateAfterWorkflow(saveDTO, LocaleContextHolder.getLocale().toString());
    }

    @Override
    public void updateBack(Long taskId, WorkflowTransferPOJO workflowTransferPOJO) throws XbbException {
        try {
            String corpid = workflowTransferPOJO.getCorpid();
            Long backupDataId = workflowTransferPOJO.getBackupDataId();
            WorkflowBackupDataEntity workflowBackupDataEntity = workflowBackupDataModel.getByKey(backupDataId, corpid);

            if (Objects.nonNull(workflowBackupDataEntity)) {

                WorkflowWorkOrderV2UpdateBackDTO updateBackDTO = new WorkflowWorkOrderV2UpdateBackDTO(XbbRefTypeEnum.RECEIPT_ORDER.getCode(), taskId, workflowTransferPOJO, workflowBackupDataEntity);
                Locale locale = LocaleContextHolder.getLocale();
                String lang = locale.toString();
                XbbResponse<WorkflowWorkOrderV2UpdateBackVO> workflowWorkOrderV2UpdateBackVOXbbResponse = workflowWorkOrderV2FeignClient.updateBack(updateBackDTO, lang);

                WorkflowWorkOrderV2UpdateBackVO answerBack = workflowWorkOrderV2UpdateBackVOXbbResponse.getResult();
                PaasFormDataEntity serviceProjectEntity = answerBack.getPaasFormDataEntityExt();
                // 回退被删掉的子表单
                updateBackSubForm(serviceProjectEntity, corpid, IndexTypeEnum.IDX_RECEIPT_ORDER_SUB);
                //回退被删掉的富文本
                updateBackRichText(taskId, workflowTransferPOJO);
            }
        } catch (Exception e) {
            LOG.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowReceiptOrderStrategy.updateBack error ", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }

    @Override
    public void approvalFailed(Long taskId, WorkflowTransferPOJO workflowTransferPOJO, Integer taskType) throws XbbException {
        try {
            String corpid = workflowTransferPOJO.getCorpid();
            Data workflowData = workflowTransferPOJO.getData();
            Long dataId = workflowData.getId();
            Long formId = workflowData.getFormId();
            Integer businessType = workflowData.getBusinessType();
            Integer saasMark = workflowData.getSaasMark();

            WorkflowWorkOrderV2ApprovalFailedDTO approvalFailedDTO = new WorkflowWorkOrderV2ApprovalFailedDTO(XbbRefTypeEnum.RECEIPT_ORDER.getCode(), taskId, taskType, workflowTransferPOJO);
            Locale locale = LocaleContextHolder.getLocale();
            String lang = locale.toString();
            workflowWorkOrderV2FeignClient.approvalFailed(approvalFailedDTO, lang);

            // 子表单
            DelEnum del = DelEnum.APPROVE_FAILED;
            paasFormSubDataModel.deleteBatchByDataId(Collections.singletonList(dataId), corpid, IndexTypeEnum.IDX_RECEIPT_ORDER_SUB, del);

            //富文本
            deleteFormRichText(dataId, corpid, formId, businessType, saasMark, DelEnum.NORMAL);

        } catch (Exception e) {
            LOG.error("com.xbongbong.workflow.service.strategy.data.impl.WorkflowAssembleStrategy.approvalFailed error ", e);
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001);
        }
    }
}
